package com.abc.widget;

import java.util.List;
import java.util.Map;

import com.abc.easyclaim.MainApplication;
import com.abc.easyclaim.R;
import com.abc.easyclaim.TextListActivity;
import com.abc.easyclaim.TextViewActivity;


import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class TextListViewAdapter extends BaseAdapter{

	private Context context; 
    private LayoutInflater listContainer; //视图容器
    private List<Map<String, Object>> listItems;
    
    public final class ListItemView{                //自定义控件集合     
        public ImageView image;     
        public TextView title;     
        public TextView info;   
        public CheckBox check;   
        public Button detailBtn;  //按钮        
    } 
    
    public TextListViewAdapter(Context context,List<Map<String, Object>> listItems){
    	this.context=context;
    	this.listItems = listItems;
        this.listContainer = LayoutInflater.from(context);
    }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listItems.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub   
        Log.e("method", "getView");   
        final int selectID = position;   
        //自定义视图   
        ListItemView  listItemView = null;   
        if (convertView == null) {   
            listItemView = new ListItemView();    
            //获取list_item布局文件的视图   
            convertView = listContainer.inflate(R.layout.list_item_text, null);   
            //获取控件对象   
            listItemView.image = (ImageView)convertView.findViewById(R.id.imageItem);   
            listItemView.title = (TextView)convertView.findViewById(R.id.titleItem);   
            listItemView.info = (TextView)convertView.findViewById(R.id.infoItem);   
            listItemView.detailBtn= (Button)convertView.findViewById(R.id.detailItem);   
            //设置控件集到convertView   
            convertView.setTag(listItemView);   
        }else {   
            listItemView = (ListItemView)convertView.getTag();   
        }   

           
        
        listItemView.title.setText((String) listItems.get(position)   
                .get("title"));   
        listItemView.info.setText((String) listItems.get(position).get("info"));   
        
        //注册按钮点击时事件
        listItemView.detailBtn.setOnClickListener(new View.OnClickListener() {   
            @Override  
            public void onClick(View v) {   
                //跳转到详情界面 
            	Intent intent = new Intent(MainApplication.getContext(), TextViewActivity.class);
            	Integer id = (Integer) listItems.get(selectID).get("id"); //文本的ID
            	intent.putExtra("textId", id);
            	MainApplication.getContext().startActivity(intent);
            }   
        });   
        
           
        return convertView;   
	}

}
